package com.example.yunbiji0314.dao;

import com.example.yunbiji0314.pojo.Good;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface GoodDao {

    //添加已加入购物车的文章
    @SelectKey(keyProperty = "id", resultType = Integer.class, before = false, statement = "select last_insert_id()")
    @Insert("insert into good(aid,uid,count) values(#{aid},#{uid},#{count})")
    int zenggood(Good good);

    //根据文章id和用户id查询商品
    @Select("select * from good where aid = #{aid} and uid = #{uid}")
    Good findById(Good good);

    //根据文章id和用户id修改文章数量
    @Update("update good set count = #{count} ,deleted=0 where aid = #{aid} and uid = #{uid}")
    int updatecount(Good good);

    //根据用户id查询购物车中的所有文章
    @Select("select article.id,article.title,article.cover_img,article.price,good.count\n" +
            "from article,good\n" +
            "where article.id=good.aid and good.deleted=0\n" +
            "and uid=#{id}")
    List<Map> findByUserId(Integer id);

    //更新商品库存
    @Update("update good set deleted=1 where aid = #{aid}")
    int gengxin(Integer aid);
}
